/**
 * Created by aa on 2016/9/12.
 */

var request = require('request');
var logger = require('../app').logger('userMsg');
var backEndUrl = require('../nodeConfig/environmentConfig');
class HttpOperate {

    constructor(host) {
        if (host == undefined) {
            this.host = backEndUrl.productBackEndUrl;
        }
        else {
            this.host = host;
        }
    };

    httpPost(url, contents, callBack, errorCallBack) {

        var reqoptions = {
            method: 'POST',
            uri: this.host + url,
            body: JSON.stringify(contents),
            headers: {
                'Content-Type': 'application/json'
            }
        };
        request(reqoptions, function (err, res, body) {
            if (err) {
                // res.redirect('http://www.baidu.com');
                var result = {
                    resultCode: err.code,
                    msg: '服务器错误！'
                };

                logger.error(err);
                //错误回调
                /* if(errorCallBack){
                 errorCallBack(result);
                 }*/
                callBack(result);
            }
            else {
                if (res.statusCode == 200) {
                    var result = JSON.parse(body);
                    callBack(result);
                }
                else {
                    var result = {
                        resultCode: res.statusCode,
                        msg: '服务器错误！'
                    };
                    logger.error(result);

                    //错误回调
                    /*if(errorCallBack){
                     errorCallBack(result);
                     }*/
                    callBack(result);
                }
            }
        });
    }

    httpPut(url, contents, callBack, errorCallBack) {

        var reqoptions = {
            method: 'PUT',
            uri: this.host + url,
            body: JSON.stringify(contents),
            headers: {
                'Content-Type': 'application/json'
            }
        };
        request(reqoptions, function (err, res, body) {
            if (err) {
                // res.redirect('http://www.baidu.com');
                var result = {
                    resultCode: err.code,
                    msg: '服务器错误！'
                };

                logger.error(err);
                //错误回调
                /* if(errorCallBack){
                 errorCallBack(result);
                 }*/
                callBack(result);
            }
            else {
                if (res.statusCode == 200) {
                    var result = JSON.parse(body);
                    callBack(result);
                }
                else {
                    var result = {
                        resultCode: res.statusCode,
                        msg: '服务器错误！'
                    };
                    logger.error(result);

                    //错误回调
                    /*if(errorCallBack){
                     errorCallBack(result);
                     }*/
                    callBack(result);
                }
            }
        });
    }

    httpGet(url, callBack, errorCallBack) {
        var reqoptions = {
            method: 'GET',
            uri: this.host + url,
            headers: {
                'Content-Type': 'application/json'
            }
        };
        request(reqoptions, function (err, res, body) {
            if (err) {
                var result = {
                    resultCode: err.code,
                    msg: '服务器错误！'
                };

                //错误回调
                /*if(errorCallBack){
                 errorCallBack(result);
                 }*/
                logger.error(err);
                callBack(result);
            }
            else {
                if (res.statusCode == 200) {
                    var result = JSON.parse(body);
                    callBack(result);
                }
                else {
                    var result = {
                        resultCode: res.statusCode,
                        msg: '服务器错误！'
                    };
                    logger.error(result);
                    callBack(result);

                    //错误回调
                    /*if(errorCallBack){
                     errorCallBack(result);
                     }*/
                }
            }
        });

    }
}

module.exports = HttpOperate;

